using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using AspNetForums.Components;

namespace AspNetForums.Admin
{
	/// <summary>
	/// Summary description for ManageSmilies.
	/// </summary>
	public class ManageSmilies : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataList dlManageSmilies;
	
		private void Page_Load(object sender, System.EventArgs e)
		{

			this.dlManageSmilies.EditCommand +=new DataListCommandEventHandler(dlManageSmilies_EditCommand);
			this.dlManageSmilies.CancelCommand +=new DataListCommandEventHandler(dlManageSmilies_CancelCommand);
			this.dlManageSmilies.DeleteCommand +=new DataListCommandEventHandler(dlManageSmilies_DeleteCommand);
			this.dlManageSmilies.UpdateCommand +=new DataListCommandEventHandler(dlManageSmilies_UpdateCommand);

			// Put user code to initialize the page here
			this.dlManageSmilies.DataSource = Smilies.GetSmilies();

			if( !Page.IsPostBack)
				dlManageSmilies.DataBind();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion

		private void dlManageSmilies_EditCommand(object source, DataListCommandEventArgs e) {
			this.dlManageSmilies.EditItemIndex = e.Item.ItemIndex;
			dlManageSmilies.DataBind();
		}

		private void dlManageSmilies_CancelCommand(object source, DataListCommandEventArgs e) {
			this.dlManageSmilies.EditItemIndex = -1;
			dlManageSmilies.DataBind();
		}

		private void dlManageSmilies_DeleteCommand(object source, DataListCommandEventArgs e) {
			Smiley smiley = (Smiley)((ArrayList)dlManageSmilies.DataSource)[e.Item.ItemIndex];

			Smilies.DeleteSmiley( smiley );
			dlManageSmilies.EditItemIndex = -1;
			dlManageSmilies.DataBind();
		}

		private void dlManageSmilies_UpdateCommand(object source, DataListCommandEventArgs e) {
			TextBox txtSmileyCode	= (TextBox)e.Item.FindControl("txtSmileyCode");
			TextBox txtSmileyText	= (TextBox)e.Item.FindControl("txtSmileyText");
			TextBox txtSmileyUrl	= (TextBox)e.Item.FindControl("txtSmileyUrl");

			if( txtSmileyCode	== null ||
				txtSmileyText	== null ||
				txtSmileyUrl	== null )
				return;

			Smiley smiley = (Smiley)((ArrayList)dlManageSmilies.DataSource)[e.Item.ItemIndex];

			smiley.SmileyCode = txtSmileyCode.Text;
			smiley.SmileyText = txtSmileyText.Text;
			smiley.SmileyUrl = txtSmileyUrl.Text;

			Smilies.UpdateSmiley( smiley );

			dlManageSmilies.EditItemIndex = -1;
			dlManageSmilies.DataBind();
		}
	}
}
